package com.android.util;

import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by jiaxiaojun on 2016/7/15.
 */
public class StringUtils {

	public final static String UTF_8 = "utf-8";

	/** 判断字符串是否有值，如果为null或者是空字符串或者只有空格或者为"null"字符串，则返回true，否则则返回false */
	public static boolean isEmpty(String value) {
		if (value != null && !"".equalsIgnoreCase(value.trim()) && !"null".equalsIgnoreCase(value.trim())) {
			return false;
		} else {
			return true;
		}
	}

	/** 判断多个字符串是否相等，如果其中有一个为空字符串或者null，则返回false，只有全相等才返回true */
	public static boolean isEquals(String... agrs) {
		String last = null;
		for (int i = 0; i < agrs.length; i++) {
			String str = agrs[i];
			if (isEmpty(str)) {
				return false;
			}
			if (last != null && !str.equals(last)) {
				return false;
			}
			last = str;
		}
		return true;
	}

	/**
	 * 返回一个高亮spannable
	 * @param content 文本内容
	 * @param color   高亮颜色
	 * @param start   起始位置
	 * @param end     结束位置
	 * @return 高亮spannable
	 */
	public static CharSequence getHighLightText(String content, int color, int start, int end) {
		if (TextUtils.isEmpty(content)) {
			return "";
		}
		start = start >= 0 ? start : 0;
		end = end <= content.length() ? end : content.length();
		SpannableString spannable = new SpannableString(content);
		CharacterStyle span = new ForegroundColorSpan(color);
		spannable.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		return spannable;
	}

	/**
	 * 获取链接样式的字符串，即字符串下面有下划线
	 * @param resId 文字资源
	 * @return 返回链接样式的字符串
	 */
	public static Spanned getHtmlStyleString(int resId) {
		StringBuilder sb = new StringBuilder();
		sb.append("<a href=\"\"><u><b>").append(UIUtils.getString(resId)).append(" </b></u></a>");
		return Html.fromHtml(sb.toString());
	}

	/** 格式化文件大小，不保留末尾的0 */
	public static String formatFileSize(long len) {
		return formatFileSize(len, false);
	}

	/** 格式化文件大小，保留末尾的0，达到长度一致 */
	public static String formatFileSize(long len, boolean keepZero) {
		String size;
		DecimalFormat formatKeepTwoZero = new DecimalFormat("#.00");
		DecimalFormat formatKeepOneZero = new DecimalFormat("#.0");
		if (len < 1024) {
			size = String.valueOf(len + "B");
		} else if (len < 10 * 1024) {
			// [0, 10KB)，保留两位小数
			size = String.valueOf(len * 100 / 1024 / (float) 100) + "KB";
		} else if (len < 100 * 1024) {
			// [10KB, 100KB)，保留一位小数
			size = String.valueOf(len * 10 / 1024 / (float) 10) + "KB";
		} else if (len < 1024 * 1024) {
			// [100KB, 1MB)，个位四舍五入
			size = String.valueOf(len / 1024) + "KB";
		} else if (len < 10 * 1024 * 1024) {
			// [1MB, 10MB)，保留两位小数
			if (keepZero) {
				size = String.valueOf(formatKeepTwoZero.format(len * 100 / 1024 / 1024 / (float) 100)) + "MB";
			} else {
				size = String.valueOf(len * 100 / 1024 / 1024 / (float) 100) + "MB";
			}
		} else if (len < 100 * 1024 * 1024) {
			// [10MB, 100MB)，保留一位小数
			if (keepZero) {
				size = String.valueOf(formatKeepOneZero.format(len * 10 / 1024 / 1024 / (float) 10)) + "MB";
			} else {
				size = String.valueOf(len * 10 / 1024 / 1024 / (float) 10) + "MB";
			}
		} else if (len < 1024 * 1024 * 1024) {
			// [100MB, 1GB)，个位四舍五入
			size = String.valueOf(len / 1024 / 1024) + "MB";
		} else {
			// [1GB, ...)，保留两位小数
			size = String.valueOf(len * 100 / 1024 / 1024 / 1024 / (float) 100) + "GB";
		}
		return size;
	}
	/**
	 * 生成data参数字符串
	 * @param dataMap data集合
	 * @return
	 */
	public static String getData(HashMap<String,String> dataMap){
		Set<Map.Entry<String, String>> entrys = dataMap.entrySet();
		StringBuilder basestring = new StringBuilder();
		for (Map.Entry<String, String> param : entrys) {
			basestring.append(param.getKey()).append("=").append(param.getValue()).append("&");
		}
		return basestring.substring(0,basestring.length()-1);
	}

	/**
	 * 翻转字符串
	 * @param str
	 * @return
     */
	public static String getRevers(String str){
		StringBuffer sb=new StringBuffer(str);
		return sb.reverse().toString();
	}

	/** 字符串转int*/
	public static int parseInt(String str) {
		if (TextUtils.isDigitsOnly(str)) {
			int result = 0;
			try {
				result = Integer.parseInt(str);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
			return result;
		}
		return 0;
	}
	public static String getTime(String current, String oldTime) {
		int day;
		int hour;
		int min;
		try {
			long time = sdf.parse(oldTime).getTime();
			long currentTime = sdf.parse(current).getTime();
			long mill = currentTime - time;

			day = (int) (mill / (24 * 60 * 60 * 1000));
			hour = Math.round(mill / (60 * 60 * 1000f) - day * 24);
			min = (int) ((mill / (60 * 1000)) - day * 24 * 60 - hour * 60);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		if (day < 1) {
			if (hour < 1) {
				return "不足1小时";
			} else {
				if (hour == 24) {
					return day + 1 + "天";
				} else {
					return hour + "小时";
				}
			}
		} else if (day >= 1) {
			if (hour < 1) {
				return day + "天";
			} else {
				if (hour == 24) {
					return day + 1 + "天";
				} else {

					return day + "天" + hour + "小时";
				}
			}
		}
		return "";
	}

	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 处理double类型末尾的0
	 *
	 * @param s
	 * @return
	 */
	public static String subZeroAndDot(String s) {
		if (s.indexOf("") > 0) {
			s = s.replaceAll("0+?$", "");//去掉多余的0
			s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
		}
		return s;
	}
	/**
	 * 用于即时建议
	 *
	 * @param type
	 * @param direction
	 * @return
	 */
	public static String opeartion(int type, String direction) {
		String result = "";
		switch (type) {
			case 0:
				result = "建仓 ";
				break;
			case 1:
				result = "减持";
				break;
			case 2:
				if ("收货(买入)".equals(direction) || "交货(卖出)".equals(direction)) {
					result = "已回购";
				} else if ("做多".equals(direction) || "做空".equals(direction)) {
					result = "平仓";
				} else {
					result = "";
				}
				break;
			case 3:
				result = "撤单";
				break;
			case 4:
				result = "修改价位";
				break;
		}
		return result;
	}
	/**
	 * 文字直播 方向的颜色处理
	 *
	 * @param direction
	 * @return
	 */
	public static String getDirectonColor(String direction) {
		if (StringUtils.isEmpty(direction)) {
			return "";
		}
		if ("收货(买入)".equals(direction) || "做多".equals(direction)) {
			return "#ff0000";
		} else if ("交货(卖出)".equals(direction) || "做空".equals(direction)) {
			return "#2f8e00";
		} else {
			return "#ffffff";
		}
	}

}
